package com.yt.setclaim.server.api.cms;

import com.yt.common.constant.Result;
import com.yt.setclaim.models.entity.FinancialPay;
import com.yt.setclaim.models.service.FinancialPayService;
import com.yt.setclaim.models.vo.FinancialPayVO;
import com.yt.setclaim.models.vo.ClaimCalcVO;
import com.yt.setclaim.models.dto.FinancialPayDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/cms/setclaim-financial-pay")
public class FinancialPayController {

    @Autowired
    private FinancialPayService financialPayService;

    /**
     * 获取所有财务支付记录
     */
    @GetMapping("/list")
    public Result<List<FinancialPayVO>> list() {
        List<FinancialPay> list = financialPayService.list();
        List<FinancialPayVO> voList = list.stream().map(financialPay -> {
            FinancialPayVO vo = new FinancialPayVO();
            BeanUtils.copyProperties(financialPay, vo);
            // ID转换为字符串类型
            vo.setPayId(String.valueOf(financialPay.getPayId()));
            vo.setCalcId(String.valueOf(financialPay.getCalcId()));
            vo.setAdminId(String.valueOf(financialPay.getAdminId()));
            // 业务字段转换为字符串类型
            if (financialPay.getPayAmount() != null) {
                vo.setPayAmount(String.valueOf(financialPay.getPayAmount()));
            }
            if (financialPay.getAdjustAmount() != null) {
                vo.setAdjustAmount(String.valueOf(financialPay.getAdjustAmount()));
            }
            return vo;
        }).collect(Collectors.toList());
        return Result.success(voList);
    }
    
    /**
     * 获取待支付的理赔核算记录
     */
    @GetMapping("/pending-list")
    public Result<List<ClaimCalcVO>> getPendingClaims() {
        return Result.success(financialPayService.getPendingClaims());
    }

    /**
     * 根据ID获取财务支付记录
     */
    @GetMapping("/info/{id}")
    public Result<FinancialPayVO> info(@PathVariable("id") Integer id) {
        FinancialPay financialPay = financialPayService.getById(id);
        FinancialPayVO vo = new FinancialPayVO();
        BeanUtils.copyProperties(financialPay, vo);
        // ID转换为字符串类型
        vo.setPayId(String.valueOf(financialPay.getPayId()));
        vo.setCalcId(String.valueOf(financialPay.getCalcId()));
        vo.setAdminId(String.valueOf(financialPay.getAdminId()));
        return Result.success(vo);
    }

    /**
     * 根据核算ID获取财务支付记录
     */
    @GetMapping("/info-by-calc/{calcId}")
    public Result<FinancialPayVO> infoByCalcId(@PathVariable("calcId") Integer calcId) {
        FinancialPay financialPay = financialPayService.getByCalcId(calcId);
        if (financialPay == null) {
            return Result.fail("未找到对应的财务支付记录");
        }
        FinancialPayVO vo = new FinancialPayVO();
        BeanUtils.copyProperties(financialPay, vo);
        // ID转换为字符串类型
        vo.setPayId(String.valueOf(financialPay.getPayId()));
        vo.setCalcId(String.valueOf(financialPay.getCalcId()));
        vo.setAdminId(String.valueOf(financialPay.getAdminId()));
        return Result.success(vo);
    }

    /**
     * 添加财务支付记录
     */
    @PostMapping("/save")
    public Result<String> save(@RequestBody FinancialPayDTO financialPayDTO) {
        FinancialPay financialPay = new FinancialPay();
        BeanUtils.copyProperties(financialPayDTO, financialPay);
        // ID转换为Integer类型
        if (financialPayDTO.getPayId() != null) {
            financialPay.setPayId(Integer.valueOf(financialPayDTO.getPayId()));
        }
        if (financialPayDTO.getCalcId() != null) {
            financialPay.setCalcId(Integer.valueOf(financialPayDTO.getCalcId()));
        }
        if (financialPayDTO.getAdminId() != null) {
            financialPay.setAdminId(Integer.valueOf(financialPayDTO.getAdminId()));
        }
        financialPayService.save(financialPay);
        return Result.success("添加成功");
    }

    /**
     * 修改财务支付记录
     */
    @PostMapping("/update")
    public Result<String> update(@RequestBody FinancialPayDTO financialPayDTO) {
        FinancialPay financialPay = new FinancialPay();
        BeanUtils.copyProperties(financialPayDTO, financialPay);
        // ID转换为Integer类型
        if (financialPayDTO.getPayId() != null) {
            financialPay.setPayId(Integer.valueOf(financialPayDTO.getPayId()));
        }
        if (financialPayDTO.getCalcId() != null) {
            financialPay.setCalcId(Integer.valueOf(financialPayDTO.getCalcId()));
        }
        if (financialPayDTO.getAdminId() != null) {
            financialPay.setAdminId(Integer.valueOf(financialPayDTO.getAdminId()));
        }
        financialPayService.updateById(financialPay);
        return Result.success("修改成功");
    }

    /**
     * 删除财务支付记录
     */
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody FinancialPayDTO financialPayDTO) {
        financialPayService.removeById(Integer.valueOf(financialPayDTO.getPayId()));
        return Result.success("删除成功");
    }
}